<?php
// $Id$

/*
 * Copyright 2011 Google Inc.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/**
 * @file
 * Admins the callbacks of the Google video sitemaps menu
 */

module_load_include('inc', 'googlevideositemaps', 'googlevideositemaps.rebuild');


/**
 * Forms creation for configuring the video sitemaps files settings
 * 
 * @see system_settings_form()
 */
 
function googlevideositemaps_admin_settings_form() { 
	//Field for defining the file name pattern specific for the site	
	$form['googlevideositemaps_file_pattern'] = array( 
		'#type' => 'textfield', 
		'#title' => t('File name pattern'), 
		'#default_value' => variable_get('googlevideositemaps_file_pattern', 'videos'), 
		'#description' => t('Pattern for the video sitemaps files. All the files will finish with "_videositemap_####.xml" where #### is a secuential number')
	);
	
	//Retrieves the sitemaps files and show them in a table view
	$files = googlevideositemaps_rebuild_list();
	
	foreach($files as $k => $file){
		$form['googlevideositemaps_files'][$k]['name']=array(
      'data' => array(
        '#type' => 'link',
        '#title' => $file['name'],
        '#href' => $file['location']
      )
    );
    
    $form['googlevideositemaps_files'][$k]['size']=array('#value' => $file['size']);
    $form['googlevideositemaps_files'][$k]['updated']=array('#value' => $file['updated']);
	}

	//add the default button for saving the changes
	$form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Configuration'),
  );
	$form['#submit'][] = 'system_settings_form_submit';
  
	return $form;
}

/**
 * Rebuilds the sitemaps of the site
 */ 
function googlevideositemaps_rebuild_form() { 
	$form['googlevideositemaps_force_rebuild'] = array( 
		'#type' => 'checkbox',
    '#title' => t('Force rebuild?'),
    '#default_value' => 0,
		'#description' => t('Check if you want to force the rebuild process.')
	);
	
	return confirm_form(
    $form,
    t('Are you sure you want to rebuild the video sitemaps?'),
    'admin/settings/googlevideositemaps/general',
    '',
    t('Rebuild sitemaps'),
    t('Cancel')
  );
}

/**
 * Submit handler; Rebuilds the sitemaps files.
 * @see googlevideositemaps_rebuild()
 */
function googlevideositemaps_rebuild_form_submit($form, &$form_state) {
  googlevideositemaps_rebuild($form);
}